package cn.ydt.ydt.work.webapi.service;

import cn.ydt.ydt.common.exception.YdtServiceException;
import cn.ydt.ydt.common.resful.JsonPage;
import cn.ydt.ydt.common.resful.StatusCode;
import cn.ydt.ydt.pojo.work.dto.WorkDTO;
import cn.ydt.ydt.pojo.work.model.Work;
import cn.ydt.ydt.pojo.work.vo.WorkDetailsVO;
import cn.ydt.ydt.pojo.work.vo.WorkSimpleVO;
import cn.ydt.ydt.user.service.IVolunteerService;
import cn.ydt.ydt.work.webapi.mapper.WorkMapper;
import cn.ydt.ydt.work.webapi.utils.SecurityContextUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import service.IWorkService;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

import static java.util.stream.Collectors.toList;

@Service
@Slf4j
@DubboService
public class WorkService implements IWorkService {
    @Autowired
    private WorkMapper workMapper;

    @DubboReference
    IVolunteerService iVolunteerService;

    //增加一个任务
    @Override
    public void addNewWork(WorkDTO workDTO) {
        Work work = new Work();
        // 调用BeanUtils.copyProperties()将参数对象中的属性值复制到Work对象中
        BeanUtils.copyProperties(workDTO, work);
        //获得正在做任务的志愿者列表，生成集合并去除重复的元素
        List<Integer> list1 = workMapper.listVolunteer().stream().distinct().collect(toList());
        //获得所有志愿者列表
        List<Integer> list2 = iVolunteerService.volunteerUserId();
        //找出没有任务的志愿者
        // 差集 (list2 - list1)
        List<Integer> list3 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
        Integer num;
        if (list3 == null) {
            //没有空的志愿者就在所有志愿者里随机选一个
            num = list2.get(new Random().nextInt(list2.size()));
        }else{
            //从有空的志愿者集合中随机一个元素
            num = list3.get(new Random().nextInt(list3.size()));
        }
        long userId = num;
        work.setUserId(userId);
        Date time = new Date();
        work.setStartTime(time);
        work.setIsFinish((byte) 0);
        work.setIsDeleted(false);
        work.setCreateTime(time);
        work.setVolunteerId(num.longValue());
        workMapper.addWork(work);
    }

    //查询任务简单信息
    @Override
    public WorkSimpleVO getByWorkId(Long workId) {
        WorkSimpleVO workSimpleVO = workMapper.getByWorkId(workId);
        if (workSimpleVO == null) {
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "获取任务失败，尝试访问的数据不存在！");
        }
        return workSimpleVO;
    }

    //查询任务详情
    @Override
    public WorkDetailsVO getDetailsWork(Long workId) {
        WorkDetailsVO WorkDetailsVO = workMapper.getDetailsWork(workId);
        if (WorkDetailsVO == null) {
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "获取任务失败，尝试访问的数据不存在！");
        }
        return WorkDetailsVO;
    }

    //根据workId完成任务
    @Override
    public void finish(Long workId) {
        WorkDetailsVO workDetailsVO = workMapper.getDetailsWork(workId);
        Byte bt = workDetailsVO.getIsFinish();
        if (bt == 1) {
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "任务已完成！");
        }
        Date Time = new Date();
        workMapper.finishWork(workId, Time, Time);
    }

    //根据workId删除任务
    @Override
    public void deleteByWorkID(Long workId) {
        WorkSimpleVO workSimpleVO = workMapper.getByWorkId(workId);
        if (workSimpleVO == null) {
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "删除任务失败，尝试访问的数据不存在！");
        }
        Date Time = new Date();
        workMapper.deleteByWorkID(workId, Time);
    }

    //删除全部完成任务
    @Override
    public void deleteAllFinishWork() {
        int row = workMapper.deleteAllWork();
        if (row == 0) {
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "任务已全部删除！");
        }
    }

    //分配任务
    @Override
    public void giveWork(Long workId, Long volunteerId) {
        Date Time = new Date();
        workMapper.setWork(workId, volunteerId, Time);
    }

    //workId分页查询任务列表
    @Override
    public JsonPage<WorkDetailsVO> listByWork(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<WorkDetailsVO> workDetailsVOS = workMapper.list();
        if (workDetailsVOS == null) {
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "服务器繁忙，请稍后再试！");
        }
        return JsonPage.restPage(new PageInfo<>(workDetailsVOS));
    }

    //    未完成列表
    @Override
    public JsonPage<WorkDetailsVO> listNotFinishByWork(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<WorkDetailsVO> workDetailsVOS = workMapper.listNotFinish();
        if (workDetailsVOS == null) {
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "服务器繁忙，请稍后再试！");
        }
        return JsonPage.restPage(new PageInfo<>(workDetailsVOS));
    }

    //userId分页查询任务列表
    @Override
    public JsonPage<WorkSimpleVO> listByUserId(Long userId, Integer page, Integer pageSize) {
        //分页信息
        PageHelper.startPage(page, pageSize);
        List<WorkSimpleVO> workSimpleVOS = workMapper.listByUserId(userId);
        if (workSimpleVOS == null) {
            throw new YdtServiceException(StatusCode.BAD_REQUEST, "服务器繁忙，请稍后再试！");
        }
        return JsonPage.restPage(new PageInfo<>(workSimpleVOS));
    }

    @Override
    public List<WorkDetailsVO> listByVolunteerId() {
        Long volunteerId = SecurityContextUtils.getUserId();
        List<WorkDetailsVO> workDetailsVO = workMapper.listByVolunteerId(volunteerId);
        return workDetailsVO;
    }
}
